package com.yunxi.controller.sat;

import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.customer.pojo.SatCustomerIntentionHisVO;
import com.yunxi.customer.service.SatCustomerIntentionHisDubboService;
import com.yunxi.dealer.pojo.PartnerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 客户意向历史 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-07-11
 */
@RestController
@RequestMapping("/admin/satCustomerIntentionHis")
@Api(tags = "sat:customerIntentionHis", description = "客户意向历史接口")
public class SatCustomerIntentionHisController {
    private static final Logger logger = LoggerFactory.getLogger(SatCustomerIntentionHisController.class);

    @Autowired
    SatCustomerIntentionHisDubboService satCustomerIntentionHisDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult page(SatCustomerIntentionHisVO satCustomerIntentionHisVO, Integer pageNum, Integer pageSize, @CurrentUser LoginInfo loginInfo) throws Exception {
        Object partner = loginInfo.getPartner();
        if(partner != null){
            PartnerVO partnerVO = new PartnerVO();
            BeanUtils.copyProperties(partnerVO,partner);
            satCustomerIntentionHisVO.setPartnerCode(partnerVO.getPartnerCode());
        }
        return ResponseUtil.ok(satCustomerIntentionHisDubboService.findPageByVO(satCustomerIntentionHisVO, pageNum, pageSize));
    }


    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    @ApiImplicitParam(paramType="query", name = "hisId", value = "主键id", required = true, dataType = "String")
    public ApiResult load(String hisId, @CurrentUser LoginInfo loginInfo) throws Exception {
        return ResponseUtil.ok(satCustomerIntentionHisDubboService.getById(hisId));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody SatCustomerIntentionHisVO satCustomerIntentionHisVO, @CurrentUser LoginInfo loginInfo) throws Exception {
        Object partner = loginInfo.getPartner();
        if(partner != null){
            PartnerVO partnerVO = new PartnerVO();
            BeanUtils.copyProperties(partnerVO,partner);
            satCustomerIntentionHisVO.setPartnerCode(partnerVO.getPartnerCode());
        }
        satCustomerIntentionHisVO.setCreateBy(loginInfo.getUserCode());
        satCustomerIntentionHisVO.setUpdateBy(loginInfo.getUserCode());
        satCustomerIntentionHisDubboService.saveOrUpdate(satCustomerIntentionHisVO);
        return ResponseUtil.ok("success");
    }

    @PostMapping("/del")
    @ApiOperation(value = "删除")
    public ApiResult del(@RequestBody List<String> list,@CurrentUser LoginInfo loginInfo) throws Exception {
        satCustomerIntentionHisDubboService.del(list,loginInfo.getUserCode());
        return ResponseUtil.ok("success");
    }

}
